package com.mailbox.mailbox.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mailbox.mailbox.domain.Type;
import com.mailbox.mailbox.mapper.TypeMapper;
import com.mailbox.mailbox.service.TypeService;
import com.mailbox.mailbox.util.ResponseResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @author 栗守佳
* @description 针对表【type】的数据库操作Service实现
* @createDate 2022-05-15 08:45:14
*/
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type>
    implements TypeService{

@Resource
TypeMapper typeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult addType(String typeName) {
        if(typeName.length()>20){
            return ResponseResult.error("问题类型长度过长");
        }
        List<Type> types = this.baseMapper.selectList(null);
        for (Type type : types) {
            if (type.getTypeName().equals(typeName)){
                return ResponseResult.error("不能添加重复的分类");
            }
        }
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date1=simpleDateFormat.format(date);
        Type type=new Type();
        type.setTypeName(typeName);
        type.setCreateTime(date1);
        this.save(type);
        return ResponseResult.success("添加成功",type);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateType(Integer typeId,String  typeName) {
        Type type=this.getById(typeId);
        if(type==null){
            return ResponseResult.error("该问题类型不存在");
        }
        if(typeName.length()>20){
            return ResponseResult.error("问题类型长度过长");
        }
        List<Type> types = this.baseMapper.selectList(null);
        for (Type typet : types) {
            if (typet.getTypeName().equals(typeName)){
                return ResponseResult.error("修改的分类名已存在");
            }
        }
        Type type1=new Type();
        type1.setTypeName(typeName);
        UpdateWrapper<Type> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("type_id",typeId);
        this.update(type1,updateWrapper);
        return ResponseResult.success("修改成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteTypeById(Integer typeId) {
        Type type=this.getById(typeId);
        if(type==null){
            return ResponseResult.error("该问题类型不存在");
        }
        this.removeById(typeId);
        return ResponseResult.success("删除成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult batchDeleteType(List<Integer> typeId) {
        this.removeBatchByIds(typeId);
        return ResponseResult.success("删除成功");
    }

    @Override
    public ResponseResult findAllType(Integer pageSize, Integer pageNow) {
        PageHelper.startPage(pageNow,pageSize);
        List<Type> typeList= list();
        PageInfo<Type> pageInfo=new PageInfo<>(typeList);
        return ResponseResult.success("查询成功",pageInfo);
    }

    @Override
    public ResponseResult findTypeByNameLike(String keyWord,Integer pageNow,Integer pageSize) {
        PageHelper.startPage(pageNow,pageSize);
        QueryWrapper<Type> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("type_name",keyWord);
        List<Type> typeList= typeMapper.selectList(queryWrapper);
        PageInfo<Type> pageInfo=new PageInfo<>(typeList);
        return ResponseResult.success("查询成功",pageInfo);
    }
}




